<?php
namespace app\admin\controller;

use app\admin\model\News as NewsModel;


use think\Db;
use Workerman\Events\React\Base;

class News extends BaseController
{

    public function index(){
        //分页

//        $list=NewsModel::with('type')->where('type_id',$type_id)->order('id desc')->paginate(3);

        //模型方法
        $model=new NewsModel();
        $list=$model->getList($this->request->param());

        $types=Db::name('news_type')->select();
        //保存多条数据

        $this->assign(compact('list','types'));

        return $this->fetch('index');

    }

    public function add(){
        $types=Db::name('news_type')->select();
        $this->assign('types',$types);
        return $this->fetch();
    }

    public function doadd(){

        $data=$this->request->param();
        $model = new NewsModel();
        $res = $model->save($data);
        if($res){
            $this->success('添加成功','news/index');
        }
        else{
            $this->error('添加失败');
        }
    }

    public function edit($id=0){
        $row=NewsModel::get($id);
        $types=Db::name('news_type')->select();
        //保存多条数据
        $this->assign(compact('row','types'));
        return $this->fetch();

    }
    public function doedit(){
        $data=$this->request->param();
        $res=NewsModel::update($data);
        if ($res) {
            $this->success('修改成功','news/index');
        }
        else {
            $this->error('修改失败');
        }
    }

    public function del($id=0)
    {
        $reg=NewsModel::destroy($id);
        if ($reg) {
            $this->success('删除成功','news/index');
        }
        else {
            $this->error('删除失败');
        }

    }

}